package com.example.sensor;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mSensor;
    
    private float mMinx;
    private float oldx;
    private float mMaxx;
    
    private float mMiny;
    private float oldy;
    private float mMaxy;
    
    private float mMinz;
    private float oldz;
    private float mMaxz;
    
    private int oldXScaled = 0;
    private int oldYScaled = 0;
    
    private int sampleCount = 0;
    
    private TextView sensorResultText;
    
    private static final float JITTER = 0.1F;
    
    private static final int MAX_X = 40;
    private static final float OFFSET_TO_ZERO = 2.0F;
    private static final int SCALE_TO_LARGE_INT = 1000;
    private static final float SCALE_UNITS_INTO_BOARD_UNITS = (((float) MAX_X) / SCALE_TO_LARGE_INT);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
         mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
         mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

         sensorResultText = (TextView) findViewById(R.id.TextView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent accel) {
		float newX = accel.values[0];
		newX = unsquare(newX);
		float newY = accel.values[1];
		newY = unsquare(newY);
		float newZ = accel.values[2];
		newZ = unsquare(newZ);
		
		if(sampleCount == 0) {
			sampleCount++;	
			
			mMinx = newX;
			oldx = newX;
			mMaxx = newX;
			
			mMiny = newY;
			oldy = newY;
			mMaxy = newY;
			
			mMinz = newZ;
			oldz = newZ;
			mMaxz = newZ;
			
			return;
		}
		
		sampleCount++;
		
//		if(isJitter(oldx, oldy, oldz, newX, newY, newZ)) {
//			return;
//		}
		
		mMinx = Math.min(mMinx, newX);
		mMaxx = Math.max(mMaxx, newX);
		
		mMiny = Math.min(mMiny, newY);
		mMaxy = Math.max(mMaxy, newY);

		mMinz = Math.min(mMinz, newZ);
		mMaxz = Math.max(mMaxz, newZ);

		if(sampleCount % 10 == 0) {
			String text = "";
			text += "Samples: " + sampleCount;
			
			sensorResultText.setText(text);
			
			sensorResultText.setLines(25);
			
			sensorResultText.append("\nminx: " + mMinx);
			sensorResultText.append("\ncurrent: " + newX);
			sensorResultText.append("\nmaxX: " + mMaxx);
			sensorResultText.append("\n");

			sensorResultText.append("\nminy:" + mMiny);
			sensorResultText.append("\ncurrent: " + newY);
			sensorResultText.append("\nmaxY: " + mMaxy);
			sensorResultText.append("\n");

			sensorResultText.append("\nminz: " + mMinz);
			sensorResultText.append("\ncurrent: " + newZ);
			sensorResultText.append("\n maxZ: " + mMaxz);
			sensorResultText.append("\n");
						
			int xScaled = (int) (((newX + OFFSET_TO_ZERO) * SCALE_TO_LARGE_INT) * SCALE_UNITS_INTO_BOARD_UNITS);
			int yScaled = (int) (((newY + OFFSET_TO_ZERO) * SCALE_TO_LARGE_INT) * SCALE_UNITS_INTO_BOARD_UNITS);
			
			sensorResultText.append("\nxScaled: " + xScaled);
			sensorResultText.append("\nyScaled: " + yScaled);
			sensorResultText.append("\n");
			
			sensorResultText.append("\nxScaled: " + xScaled);
			sensorResultText.append("\nyScaled: " + yScaled);
			sensorResultText.append("\n");
			
			int deltaX = oldXScaled - xScaled;
			int deltaY = oldYScaled - yScaled;
			
			sensorResultText.append("\ndeltaX: " + deltaX);
			sensorResultText.append("\ndeltaY: " + deltaY);
			sensorResultText.append("\nsignX: " + Math.signum(newX));
			sensorResultText.append("\nsignY: " + Math.signum(newY));

			
			oldXScaled = xScaled;
			oldYScaled = yScaled;
			
			oldx = newX;
			oldy = newY;
			oldz = newZ;

		}
	}  
	
	private boolean isJitter(float oldx, float oldy, float oldz, float newx, float newy, float newz) {
		float deltax = newx - oldx;
		float deltay = newy - oldy;
		
		if(deltax < JITTER  && deltay < JITTER) {
			return true;
		}
		
		return false;
	}
	
	private float unsquare(double number) {
		int sign = 1;
		if(number < 0) {
			sign = -1;
			number = Math.abs(number);
		}
		
		float result = (float) Math.sqrt(number);
		
		return sign * result;
	}
}
